<template>
  <div>
    <el-table
      :data="tableData"
      v-loading="loading"
      stripe
      style="width: 100%"
      max-height="700"
    >
      <el-table-column prop="code" label="编码" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.code || '--' }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="startDate"
        label="学期起始日期"
        width="120"
        v-if="type == 1"
      >
      </el-table-column>
      <el-table-column
        prop="endDate"
        label="学期结束日期"
        width="120"
        v-if="type == 1"
      >
      </el-table-column>
      <el-table-column
        prop="totalWeeks"
        label="学期总周数"
        width="120"
        v-if="type == 1"
      >
      </el-table-column>
      <el-table-column
        prop="startWeeksDate"
        label="起始周日期"
        width="120"
        v-if="type == 1"
      >
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="{ row }">
          <el-button-group>
            <!-- <el-button
              type="primary"
              size="mini"
              @click="handleCurrent(row, 2)"
              v-if="row.type == 1 && row.isCurrent == 2"
              >设置当前学期</el-button
            > -->
            <el-button type="primary" size="mini" @click="handleEdit(row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="handleDelete(row)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  eduTierQuery,
  eduTierDelete,
  eduTierSetCurrent,
} from '@/api/admin/teach/eduTier.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      loading: false,
      listQuery: {
        page: 1,
        limit: 10,
        type: this.type,
      },
      tableData: [],
      pageObj: {},
    }
  },
  props: ['type'],
  mounted() {
    this.getList()
    Bus.$on('refreshClassroomList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshClassroomList')
  },
  methods: {
    getList() {
      this.loading = true
      eduTierQuery(this.listQuery).then((res) => {
        setTimeout(() => {
          this.tableData = res.data.data
          this.pageObj = res.data
          this.loading = false
        }, 200)
      })
    },
    //编辑
    handleEdit(row) {
      Bus.$emit('showDialogClassroomAdd', row)
    },
    //设置当前学期
    handleCurrent(row) {
      eduTierSetCurrent(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      eduTierDelete(row.id).then((res) => {
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>